﻿using System;
using System.Collections.Generic;
using DataEntities;
using DataProvider;
using SQLDataProvider;

namespace BusinessServices
{
    public class CarService
    {
        private readonly IBaseRepository<CarInfo> _carRepository;
        private readonly YuZuService _yuZuService;
        private readonly ChuZuService _chuZuService;

        public CarService()
        {
            _carRepository = new BaseRepository<CarInfo>();
            _yuZuService = new YuZuService();
            _chuZuService = new ChuZuService();
        }

        /// <summary>
        /// 获取所有车辆信息
        /// </summary>
        public List<CarInfo> GetAll()
        {
            return _carRepository.GetAll(p => true);
        }

        /// <summary>
        /// 获取空闲车辆的信息
        /// </summary>
        public List<CarInfo> GetFreeCars()
        {
            var yuZuCarIds = _yuZuService.GetYuZuCarIds();
            var chuZuCarIds = _chuZuService.GetZuChuCarIds();
            return _carRepository.GetAll(p => !p.bBF && !yuZuCarIds.Contains(p.iCarid) && !chuZuCarIds.Contains(p.iCarid));
        }

        /// <summary>
        /// 获取出租中的车辆信息
        /// </summary>
        public List<CarInfo> GetRentingCars()
        {
            var chuZuCarIds = _chuZuService.GetZuChuCarIds();
            return _carRepository.GetAll(p => !p.bBF && chuZuCarIds.Contains(p.iCarid));
        }

        /// <summary>
        /// 添加新的车辆信息
        /// </summary>
        public CarInfo Add(CarInfo carInfo)
        {
            return _carRepository.Add(carInfo);
        }

        /// <summary>
        /// 更新车辆信息
        /// </summary>
        /// <param name="carId">车辆Id</param>
        /// <param name="update">执行更新的委托,在此方法中进行更新操作</param>
        public CarInfo Update(int carId, Action<CarInfo> update)
        {
            return _carRepository.Update(c => c.iCarid == carId, update);
        }

        /// <summary>
        /// 删除车辆信息
        /// </summary>
        /// <param name="carId">车辆Id</param>
        public bool Delete(int carId)
        {
            return _carRepository.Delete(c => c.iCarid == carId);
        }
    }
}
